package com.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gulimall.common.enums.AttrEnum;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.gulimall.product.dao.AttrDao;
import com.gulimall.product.entity.AttrEntity;
import com.gulimall.product.service.AttrService;


/**
 * @author ht
 */
@Service("attrService")
public class AttrServiceImpl extends ServiceImpl<AttrDao, AttrEntity> implements AttrService {

    @Override
    public IPage<AttrEntity> queryPage(Page<AttrEntity> page, AttrEntity attrGroup, String key) {
        LambdaQueryWrapper<AttrEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(key)) {
            lambdaQueryWrapper.like(AttrEntity::getAttrName, key)
                    .or().like(AttrEntity::getAttrType, key)
                    .or().like(AttrEntity::getSearchType, key)
                    .or().like(AttrEntity::getShowDesc, key)
                    .or().like(AttrEntity::getValueSelect, key);
        }
        return baseMapper.selectPage(page, lambdaQueryWrapper);
    }

    @Override
    public IPage<AttrEntity> queryPageByCateId(Page<AttrEntity> page, AttrEntity attrGroup, Long catelogId, String key, String type) {
        LambdaQueryWrapper<AttrEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (catelogId != null && catelogId != 0) {
            lambdaQueryWrapper.eq(AttrEntity::getCatelogId, catelogId);
        }
        if (StringUtils.isNotEmpty(key)) {
            lambdaQueryWrapper.like(AttrEntity::getAttrName, key)
                    .or().like(AttrEntity::getAttrType, key)
                    .or().like(AttrEntity::getSearchType, key)
                    .or().like(AttrEntity::getShowDesc, key)
                    .or().like(AttrEntity::getValueSelect, key);
        }
        Object[] attrTypes;
        if (StringUtils.isNotEmpty(type)) {
            if (AttrEnum.ATTR_TYPE_BASE.getType().equals(type)) {
                attrTypes = new Object[]{AttrEnum.ATTR_TYPE_BASE.getCode(), AttrEnum.ATTR_TYPE_BASE_AND_SALE.getCode()};
            } else {
                attrTypes = new Object[]{AttrEnum.ATTR_TYPE_SALE.getCode(), AttrEnum.ATTR_TYPE_BASE_AND_SALE.getCode()};
            }
            lambdaQueryWrapper.in(AttrEntity::getAttrType, attrTypes);
        }
        return baseMapper.selectPage(page, lambdaQueryWrapper);
    }

}